<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends BaseController{


    //会员中心首页
    public function index(){
        $auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
        if (!$auth) {
            echo "未授权或授权已过期";exit;
        }

        $userid = session('user');
        $t_id = $userid['t_id'];
        if($t_id == '0'){
            $kefu = "/Public/Home/img/kefu1.jpg";
        }else{
            $agent_info = M("user")->field("kefu_code")->where("id='$t_id'")->find();
            $kefu = "/Uploads".$agent_info['kefu_code'];
        }

        $userinfo = M('user')->where("id = {$userid['id']}")->find();

        //下线总数
        $userinfo['t_account'] = M('user')->where("t_id = {$userid['id']}")->count() ? : 0;

        //今日盈亏
        $map = array();
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

        $map['time'] = array(array('egt',$beginToday),array('elt',$endToday),'and');
        $map['state'] = 1;
        $map['userid'] = $userid['id'];

        $yinkui = M('order')->field("sum(add_points) as sum_add,sum(del_points) as sum_del")
            ->where($map)->where("is_add=1")
            ->find();
        $userinfo['ying'] = $yinkui['sum_add'] - $yinkui['sum_del'];

        //今日推广佣金
        $map_y['time'] = array(array('egt',$beginToday),array('elt',$endToday),'and');
        $map_y['t_uid'] =  $userid['id'];
        $yong = M('push_money')->where($map_y)->field("sum(money) as money")->find();

        $userinfo['yong_today'] = $yong['money'] ? : 0;
//		$kefu = M('config')->where("id = 1")->find();
//		$this->assign('kefu',$kefu);

        $is_weixin = is_weixin();
        $this->assign('is_weixin',$is_weixin);
        $this->assign('kefu', $kefu);
        $this->assign("user",$userinfo);
        $this->display();
    }

    //佣金转钱包
    public function yzqpage(){
        $this->display();
    }

    //佣金转钱包
    public function fzqpage(){
        $this->display();
    }

    public function uppwd(){
        $this->display();
    }



    //修改密码
    public function dopwd(){
        $old_pwd = I('old_pwd');
        $pwd = I('pwd');
        $user = session('user');
        $userinfo = M('user')->where("id = {$user['id']}")->find();
        if ($userinfo['password'] != md5($old_pwd)) {
            $this->error("旧密码不正确");
        }

        $res = M('user')->where("id = {$user['id']}")->save(array('password'=>md5($pwd)));

        if ($res) {
            $this->success('修改密码成功，跳转中~',U('Home/User/index'),1);
        } else {
            $this->error('修改密码失败');
        }

    }

    //佣金转钱包
    public function yzq(){
        $money = I('money');
        $uid = session('user');
        $user = M('user')->where("id = {$uid['id']}")->find();

        if (intval($user['yong']) < intval($money)) {
            $this->error("佣金不足，请确认");
        }

        $User = M('user');
        $User->startTrans();
        $res1 =  $User->where("id = {$uid['id']}")->setDec('yong',$money);
        $res2 =  $User->where("id = {$uid['id']}")->setInc('points',$money);

        if ($res1 && $res2) {
            $User->commit();
            $this->success('转入成功，跳转中~',U('Home/User/index'),1);
        } else {
            $User->rollback();
            $this->error('转入失败，请重试');
        }
    }

    //佣金转钱包
    public function fzq(){
        $money = I('money');
        $uid = session('user');
        $user = M('user')->where("id = {$uid['id']}")->find();

        if (intval($user['fanshui']) < intval($money)) {
            $this->error("返水金额不足，请确认");
        }

        $User = M('user');
        $User->startTrans();
        $res1 =  $User->where("id = {$uid['id']}")->setDec('fanshui',$money);
        $res2 =  $User->where("id = {$uid['id']}")->setInc('points',$money);

        if ($res1 && $res2) {
            $User->commit();
            $this->success('转入成功，跳转中~',U('Home/User/index'),1);
        } else {
            $User->rollback();
            $this->error('转入失败，请重试');
        }
    }

    public function off2()
    {
        $member = M('user');
        $user_list = $member->limit(3)->order("points DESC")->select();
        dump($user_list);
    }
    //我的下线
    public function offline(){
        $id = I('id');
       user_liusui($id);
//筛选时间
        $time = I('time');
        $start = strtotime(str_replace("+"," ",I('starttime')));
        $end = strtotime(str_replace("+"," ",I('endtime')));

        if($time){
            if ($time == 'today') {
                $start=mktime(0,0,0,date('m'),date('d'),date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            } elseif($time == 'yestoday') {
                $start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            } elseif($time == 'week') {
                $start=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            }  elseif($time == 'month') {
                $start=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            }
        }
        $map = array();
        if ($start && $end) {
            $map['time'] = array(array('egt',$start),array('elt',$end),'and');
        }
        // $userinfo = session('user');
        $member = M('user');
        $count = $member->where("t_id = {$id}")->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $user_list = $member->where("t_id = {$id}")->limit($page->firstRow.','.$page->listRows)->order("liushui DESC")->select();
        $arr = array();
        foreach ($user_list as $k=>$v){
            $arr[$k]['id'] = $v['id']; //用户id
            $arr[$k]['username'] = $v['username']; //用户账号（电话号码）
            $arr[$k]['reg_time'] = $v['reg_time']; //注册时间
            $arr[$k]['headimgurl'] = $v['headimgurl']; //头像
            $arr[$k]['add_points'] = 0;//进项
            $arr[$k]['del_points'] = 0;//出项
            $arr[$k]['bet']= 0;//流水
            $arr[$k]['compare_points'] = 0;//输赢
            //查询该会员所有投注
            $map['userid'] = $v['id'];
//            $arr[$k]['liusui']= $user_order = M("order")->field("sum(add_points) as sum_add,sum(del_points) as sum_del")->where("state=1")->where($map)->find();
//			$map['is_add'] = 1;
            $order_member = M("order")->where($map)->select();
            foreach($order_member as $k3=>$v3){
                $ok = $v3['add_points']-$v3['del_points'];
                if($ok>0){
                    $arr[$k]['add_points'] += $ok;
                }else{
                    if (1==$v3['is_add']){
                        $arr[$k]['del_points'] += $ok*-1;
                    }
                }
                $arr[$k]['compare_points']=$arr[$k]['add_points']-$arr[$k]['del_points'];
                $arr[$k]['bet'] += $v3['del_points'];//流水
            }
        }
        $this->assign('id',$id);
        $this->assign('show',$show);
        $this->assign('list',$arr);
        $this->assign('start',$start ?  date('Y-m-d H:i:s',$start) : '');
        $this->assign('end',$end ? date('Y-m-d H:i:s',$end) : '');
        $this->display();
    }


    private function _arraysort($arr,$field){
        $field_arr = array();
        foreach ($arr as $key=>$value)
        {
            $field_arr[$key] = $value[$field];
        }

        if(!empty($field_arr)&&!empty($arr))
        {
            array_multisort($field_arr,SORT_DESC,$arr);
        }
        return $arr;
    }

    //我的下线下注记录
    public function offxz(){
        $time = I('time');
        $map = array();

        $map['state'] = 1;
        $time = I('time');
        $start = strtotime(I('starttime'));
        $end = strtotime(I('endtime'));

        if($time){
            if ($time == 'today') {
                $start=mktime(0,0,0,date('m'),date('d'),date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            } elseif($time == 'yestoday') {
                $start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            } elseif($time == 'week') {
                $start=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            }  elseif($time == 'month') {
                $start=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            }
        }

        if ($start && $end) {
            $map['time'] = array(array('egt',$start),array('elt',$end),'and');
        }

        $id = I('id');
        $map['is_under'] = 1;
        $map['userid'] = $id;
        $order = M('order');
        // $integral = M('integral');
        $count = $order->where($map)->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = array();
        $tj = $order->field("sum(add_points) as sum_add,sum(del_points) as sum_del")->where($map)->find();
        $list = $order->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();

        $this->assign('list',$list);
        $this->assign('time',$time);
        $this->assign('id',$id);
        $this->assign('show',$show);
        $this->assign('tj',$tj);
        $this->assign('start',$start ?  date('Y-m-d',$start) : '');
        $this->assign('end',$end ? date('Y-m-d',$end) : '');
        $this->display();
    }

    //我的下线充值记录
    public function offcz(){
        $id = I('id');
        $map['uid'] = $id;

        $integral = M('integral');
        $time = I('time');
        $map = array();
        $map['uid'] = $id;
        $start = strtotime(I('starttime'));
        $end = strtotime(I('endtime'));

        if($time){
            if ($time == 'today') {
                $start=mktime(0,0,0,date('m'),date('d'),date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            } elseif($time == 'yestoday') {
                $start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            } elseif($time == 'week') {
                $start=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            }  elseif($time == 'month') {
                $start=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
                $end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            }
        }

        if ($start && $end) {
            $map['time'] = array(array('egt',$start),array('elt',$end),'and');
        }


        $sum_data = array(
            'add' => 0,
            'xia' => 0,
        );


        $count = $integral->where($map)->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $sum_add = $integral->field("sum(points) as sum_add")->where("type = 1")->where($map)->find();
        $sum_data['add'] = $sum_add['sum_add'];
        $sum_xia = $integral->field("sum(points) as sum_add")->where("type = 0")->where($map)->find();
        $sum_data['xia'] = $sum_xia['sum_add'];
        $list = $integral->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();

        for($i=0;$i<count($list);$i++){
            $list[$i]['user'] = M('user')->where("id = {$list[$i]['uid']}")->find();
        }

        $startline =  $start ? date('Y-m-d',$start) : '';
        $endline =  $start ? date('Y-m-d',$end) : '';

        $this->assign('nickname',$nickname);
        $this->assign('start',$startline);
        $this->assign('end',$endline);
        $this->assign('list',$list);
        $this->assign('id',$id);
        $this->assign('show',$show);
        $this->assign('sum_data',$sum_data);
        $this->assign('time',$time);
        $this->display();
    }

    //充值记录
    public function fenadd(){
        $uid = session('user')['id'];
        $member = M('integral');
        $count = $member->field("sum(points) as sum_points,count(id) as count")->where("type = 1  and uid = {$uid}")->find();

        $page = new \Think\Page($count['count'],10);
        $show = $page->show();
        $list = $member->where("type = 1 and uid = {$uid}")->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();

        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->assign('list',$list);
        $this->display();
    }

    //提现记录
    public function fenxia(){
        $uid = session('user')['id'];
        $member = M('integral');
        $count = $member->field("sum(points) as sum_points,count(id) as count")->where("type = 0  and uid = {$uid}")->find();
        $page = new \Think\Page($count['count'],10);
        $show = $page->show();
        $list = $member->where("type = 0 and uid = {$uid}")->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();

        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->display();
    }

    public function xiazhu(){
        $t = I('t');
        $type = I('type');
        $pkdata = F('pk10data');
        $map = array();
        if($t == 1){
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        }
        if($t == 2){
            $beginToday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        }
        if($t == 3){
            $beginToday=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')-1,date('Y'))-1;
        }
        if($t == 4){
            $beginToday=0;
            $endToday=0;
        }

        if ($beginToday and $endToday) {
            $map['time'] = array(array('egt',$beginToday),array('elt',$endToday),'and');
        }
        $userinfo = session('user');
        $map['state'] = 1;
        $map['userid'] = $userinfo['id'];
        if(!empty($type)){
            if($type == '1'){
                $map['type'] = 0;
            }else{
                $map['type'] = array('neq',0);
            }
        }
//		$map['is_add'] = 1;
        $order = M('order');
        $count = $order->where($map)->count();
        $page = new \Think\Page($count,6);
        $show = $page->show();
        $arr = array();
        $arr['bet'] = 0;
        $list = $order->field("*")->where($map)->limit($page->firstRow.','.$page->listRows)
            ->order("id DESC")->select();
        $list2 = $order->field("*")->where($map)
            ->order("id DESC")->select();
        foreach($list2 as $k3=>$v3){
            $ok = $v3['add_points']-$v3['del_points'];
            if (1==$v3['is_add']){
                if($ok>0){
                    $arr['add_points'] += $ok;
                }else{
                    $arr['del_points'] += $ok*-1;
                }
            }

            $arr['compare_points'] = $arr['add_points']-$arr['del_points'];
            $arr['bet'] += $v3['del_points'];
        }
//		$show = $page->show();
//		$count = $order->where($map)->count();
//		$points_tj = $order->field("count(id) as count,sum(add_points) as sum_add,sum(del_points) as sum_del")->where($map)->find();
//		$points_tj['ying'] = $points_tj['sum_add'] - $points_tj['sum_del'];
//		$page = new \Think\Page($points_tj['count'],6);
//
//		$show = $page->show();
//		$list = $order->field("*")->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();
//
//
        $list1 = $list;

        $this->assign('list1',$list1);
        $this->assign('state',F('state'));
        $this->assign('number',$pkdata['next']['periodNumber']);
        $this->assign('list',$list);
//		$this->assign('points_tj',$points_tj);
        $this->assign('arr',$arr);
        $this->assign('show',$show);
        $this->assign('today',mktime(0,0,0,date('m'),date('d'),date('Y')));
        $this->assign('t',$t);
        $this->display();
    }

    public function customer(){
        $info = M('config')->where(['id'=>['in','1,5,6,7']])->getField('id,kefu');
        $this->assign('info',$info);
        $this->display();
    }

}
?>